package eerbaar.v2.app.tasks;

import java.awt.Graphics2D;

import eerbaar.game.IGame;
import eerbaar.shared.service.ServiceFactory;
import eerbaar.v2.app.GameHelper;
import eerbaar.v2.app.core.IApp;
import eerbaar.v2.app.mover.IPersonaMover;

public class CheckLevelTask implements IGameTask {

    private final IApp app;

    public CheckLevelTask(IApp app) {
        this.app = app;
    }

    @Override
    public void doTask(Graphics2D g) {

        IGame game = GameHelper.getGame();
        if (game == null) {
            return;
        }

        if (game.getLevel().check()) {

            sit();
            this.app.getForges().getLevelForge().start();
            sit();

            this.app.getLoop().setSuspended(true);
        }
    }

    @Override
    public Priority getPriority() {
        return Priority.FIRST;
    }

    @Override
    public int getRate() {
        return IGameTask.STANDARD_RATE;
    }

    @Override
    public boolean runWhenSuspended() {
        return false;
    }

    private void sit() {

        IPersonaMover mover = ServiceFactory.getService(IPersonaMover.class);
        if (mover != null) {
            mover.sit();
        }
    }

}
